#include "rclcpp/rclcpp.hpp"

class ParamClient : public rclcpp::Node
{
public:
    ParamClient() : Node("demo_param_client")
    {
        RCLCPP_INFO(this->get_logger(), "节点已经启动!");
        param_client_ = std::make_shared<rclcpp::SyncParametersClient>(
            this,
            "demo_param_server");
    }
    /**
     * 连接服务端
     */
    bool connect_server()
    {
        while (!param_client_->wait_for_service(std::chrono::seconds(1)))
        {
            if (!rclcpp::ok())
            {
                RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "退出！");
                return false;
            }
            RCLCPP_INFO(this->get_logger(), "等待服务启动!");
        }
        return true;
    }

    void get_param()
    {
        RCLCPP_INFO(this->get_logger(), "获取参数!");
        auto param_list = param_client_->get_parameters({"name", "x", "y", "z"});
        for (auto &param : param_list)
        {
            RCLCPP_INFO(this->get_logger(), "key= %s,value= %s,type=%s",
                        param.get_name().c_str(),
                        param.value_to_string().c_str(),
                        param.get_type_name().c_str());
        }
    }
    void update_param()
    {
        RCLCPP_INFO(this->get_logger(), "更新参数!");
        param_client_->set_parameters({rclcpp::Parameter("name", "充电点"),
                                       rclcpp::Parameter("x", 1.0),
                                       rclcpp::Parameter("y", 2.0),
                                       rclcpp::Parameter("z", 3.0)});
    }

private:
    rclcpp::SyncParametersClient::SharedPtr param_client_;
};

int main(int argc, char *argv[])
{
    /* 初始化rclcpp  */
    rclcpp::init(argc, argv);
    /* 产生一个 节点 */
    auto node = std::make_shared<ParamClient>();
    /* 连接服务端 */
    if (!node->connect_server())
    {
        return 0;
    }
    node->get_param();
    node->update_param();
    node->get_param();
    // rclcpp::spin(node);
    /* 释放资源 */
    rclcpp::shutdown();
    return 0;
}